home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2849 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Interesting declaration???
  5. Date: 24 Jan 1996 14:56:54 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan24095654@g7240065.bridge.bst.bls.com>
  8. References: <4e3on1$iir@masala.cc.uh.edu>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: sukku@menudo.uh.edu's message of 23 Jan 1996 22:50:09 GMT
  11.  
  12. In article <4e3on1$iir@masala.cc.uh.edu> sukku@menudo.uh.edu (sukumar) writes:
  13.  
  14.        I was going through some of the header files of Motif and found an
  15. : interesting declaration like this:
  16.  
  17. : typedef enum{
  18. :           NONE,
  19. :           ONE,
  20. :           TWO} TabelType;
  21.                            ^^
  22. I'll presume this was a typo.
  23.  
  24. : #define TableType unsigned char
  25.  
  26.  
  27. : I haven't seen this kind of declartion before and I tried it after seeing this
  28. : and it worked.!! I was expecting the compiler to throw a flag. Why would
  29. : someone do this?? 
  30.  
  31. : My best guess is that they are making sure the TableType gets only one
  32. : byte instead of four (On Unix).
  33.  
  34. : Can someone throw some light on this??
  35.  
  36. There is no reason for this not to compile:
  37.  
  38.   typedef enum { NONE, ONE, TWO } TableType;
  39.  
  40.   #define TableType unsigned char
  41.  
  42.   TableType flag = ONE;
  43.  
  44. After the preprocessor stage looks like.
  45.  
  46.   typedef enum { NONE, ONE, TWO } TableType;
  47.  
  48.   unsigned char flag = ONE;
  49.  
  50. Enums are compatible with an integer type (char/short/int/long), the choice
  51. of the type is implementation defined.
  52. When the compiler sees the flag initialisation it performs a conversion
  53. (if necessary) from the enum to unsigned char.
  54. The standard states that in this instance NONE=0, ONE=1 and TWO=2
  55. so there is no problem with this.
  56.  
  57. There may be several reasons for doing this:
  58.  
  59.   Size maybe important, char types take one byte enums can take more.
  60.   If this is being used in network communication (like most of X) then
  61.     the size of the enum on one machine maybe different on another machine,
  62.     it allows one to rationalise about the its size which can't be
  63.     done with enums.
  64.   The enumeration are bit flags and require or'ing together (probably not
  65.     in this case) '|' is not defined on enums and must be converted to
  66.     an integer type.
  67.   And more...
  68.  
  69. The reason for doing it the way it was done allows users to still,
  70. declare things using TableType with out having to be care of what
  71. is actually going on.
  72.  
  73. Regards
  74.  
  75.    -A.
  76. -- 
  77. | A.Champion                |
  78.